package com.doge.calcalbum.business.home

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.core.view.setPadding
import com.doge.calcalbum.R
import com.doge.calcalbum.api.model.UserInfo
import com.doge.calcalbum.business.SettingsActivity
import com.doge.calcalbum.business.WebActivity
import com.doge.calcalbum.business.account.AppUserViewModel
import com.doge.calcalbum.business.note.NoteActivity
import com.doge.calcalbum.business.share.ShareHelper
import com.doge.calcalbum.business.vip.VipBuyActivity
import com.doge.calcalbum.business.trash.TrashActivity
import com.doge.calcalbum.business.vip.VipHelper
import com.doge.calcalbum.core.BaseFragment
import com.doge.calcalbum.core.base.isRefreshing
import com.doge.calcalbum.databinding.FragmentProfileBinding
import com.doge.calcalbum.utils.*

class ProfileFragment : BaseFragment() {
    private var innerBinding: FragmentProfileBinding? = null
    private val binding get() = innerBinding!!
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        innerBinding = innerBinding ?: FragmentProfileBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        systemWindowInsetsTop.observe(viewLifecycleOwner) {
            binding.fitsSys.setPadding(it / 2)
        }
        binding.cacheValue.text = CacheUtils.calcTotalSize(appContext)

        val skinViews = arrayOf(
            binding.privileges2,
            binding.privileges2Text,
        )
        skinViews.forEach { it.isVisible = !disableSkin }

        initialViews()

        AppUserViewModel.get().user.observe(viewLifecycleOwner) {
            updateUserInfo(it)
            activity?.let { activity -> VipHelper.tryShowWhenAppOpen(activity) }
        }
        CacheUtils.loading.observe(viewLifecycleOwner) {
            binding.cacheLoading.isVisible = it.isRefreshing()
            if (!it.isRefreshing()) binding.cacheValue.text = CacheUtils.calcTotalSize(appContext)
        }
    }

    private fun initialViews() {
        binding.layVip.setOnClickCallback { VipBuyActivity.start(it.context) }
        binding.vipTip.setOnClickCallback { VipBuyActivity.start(it.context) }
        binding.layTrash.setOnClickCallback { TrashActivity.start(it.context) }
        binding.layNote.setOnClickCallback { NoteActivity.start(activity) }
        binding.layClear.setOnClickCallback { CacheUtils.clear(appContext) }
        binding.layUserPolicy.setOnClickCallback { WebActivity.start(it.context, urlUserPolicy) }
        binding.layPrivacy.setOnClickCallback { WebActivity.start(it.context, urlPrivacy) }
        binding.layShare.setOnClickCallback { ShareHelper.shareApp() }
        binding.layService.setOnClickCallback { WxServiceHelper.show(activity)  }
        binding.laySettings.setOnClickCallback { SettingsActivity.start(it.context) }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding.layTrash.setOnClickListener(null)
        binding.layNote.setOnClickListener(null)
        binding.layClear.setOnClickListener(null)
        binding.layUserPolicy.setOnClickListener(null)
        binding.layPrivacy.setOnClickListener(null)
        binding.layService.setOnClickListener(null)
        binding.layShare.setOnClickListener(null)
        binding.laySettings.setOnClickListener(null)
        innerBinding = null
    }


    @SuppressLint("SetTextI18n")
    private fun updateUserInfo(data: UserInfo?) {
        val isVipForever = data?.vipExpireLeftTime == -1L
        val isVipUsing = isVipForever || (data?.vipExpireLeftTime ?: 0) > 0 // VIP还有时间
        if (isVipUsing) {
            binding.fitsSys.setBackgroundColor(getColor("#2B2B2B"))
            binding.top.setBackgroundColor(getColor("#2B2B2B"))
            binding.layVip.setBackgroundResource(R.drawable.shape_vip_using)
            binding.vipTimestamp.isVisible = true
            binding.vipTimestamp.text =
                if (isVipForever) getString(R.string.forever) else "将于${data?.vipExpireTimestamp?.timeDesc()}到期"
            binding.vipOpenText.setText(R.string.vip_renew)
            binding.vipOpenText.isVisible = !isVipForever
        } else {
            binding.fitsSys.setBackgroundColor(getColor("#5359A7"))
            binding.top.setBackgroundColor(getColor("#5359A7"))
            binding.layVip.setBackgroundResource(R.drawable.shape_vip_normal)
            binding.vipTimestamp.isVisible = false
            binding.vipOpenText.setText(R.string.vip_open)
            binding.vipOpenText.isVisible = true
        }
    }
}